package com.quectel.cms.controller.device;


import com.quectel.cms.controller.BaseController;
import com.quectel.cms.vo.device.DeviceCategoryIconSetVo;
import com.quectel.util.aspect.log.annotation.Log;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.constant.global.annotation.RequireTenants;
import com.quectel.constant.sys.SysConstants;
import com.quectel.core.module.device.dto.DeviceCategoryDto;
import com.quectel.core.module.device.service.DeviceCategoryService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-05-31 12:10:19
 */
@RestController
@RequestMapping("deviceCategory")
@Tag(name = "DeviceCategoryController")
@Tag(name = "DeviceCategoryController", description = "设备种类相关api")
public class DeviceCategoryController extends BaseController {

    @DubboReference
    private DeviceCategoryService deviceCategoryService;

    /**
     * 查询所有的种类
     */
    @GetMapping("queryAll")
    @Operation(summary = "设备种类的查询列表")
    public Response<List<DeviceCategoryDto>> queryAll() {
        Map<String, Object> params = Request.parseParams(httpServletRequest);
        List<DeviceCategoryDto> list = deviceCategoryService.queryList(params);
        return Response.<List<DeviceCategoryDto>>ok().wrap(list);
    }

    @GetMapping("queryByCode")
    @Operation(summary = "通过code查询设备种类")
    public Response<DeviceCategoryDto> queryByCode(String code) {
        DeviceCategoryDto deviceCategoryDto = deviceCategoryService.selectByCode(code);
        return Response.<DeviceCategoryDto>ok().wrap(deviceCategoryDto);
    }


    /**
     * 列表
     */
    @GetMapping("list")
//    @RequirePermissions(values ="deviceCategory:list")
    @Operation(summary = "设备种类的分页查询列表")
    public Response<Page<DeviceCategoryDto>> list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Request request = Request.configParams(params).like("name");

        List<DeviceCategoryDto> list = deviceCategoryService.queryList(params);
        int total = deviceCategoryService.queryTotal(params);

        Page<DeviceCategoryDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<DeviceCategoryDto>>ok().wrap(page);
    }


    /**
     * 信息
     */
    @GetMapping("info/{id}")
//    @RequirePermissions(values ="deviceCategory:info")
    @Operation(summary = "查询单条设备种类记录")
    public Response<DeviceCategoryDto> info(@PathVariable("id") Long id) {

        DeviceCategoryDto deviceCategoryDto = deviceCategoryService.selectById(id);

        return Response.<DeviceCategoryDto>ok().wrap(deviceCategoryDto);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "deviceCategory:edit")
    @RequireTenants(values = SysConstants.SYS_TOP_TENANT_CODE)
    @Operation(summary = "保存单条设备种类记录")
    @Log(express = "保存设备种类")
    public Response<Object> save(@RequestBody @Validated DeviceCategoryDto deviceCategoryDto) {

        DeviceCategoryDto dbValue = deviceCategoryService.selectByCode(deviceCategoryDto.getCode());
        if (dbValue != null) {
            return Response.error("该设备种类编码已存在");
        }

        //填充基础数据
        fillSaveCommonData(deviceCategoryDto);

        deviceCategoryService.save(deviceCategoryDto);

        return Response.ok();
    }


    /**
     * 保存
     */
    @PostMapping("iconSet")
    @RequirePermissions(values = "deviceCategory:edit")
    @RequireTenants(values = SysConstants.SYS_TOP_TENANT_CODE)
    @Operation(summary = "查询单条设备种类的icon信息")
    @Log(express = "保存设备种类")
    public Response<Object> iconSet(@RequestBody DeviceCategoryIconSetVo deviceCategoryIconSetVo) {

        DeviceCategoryDto deviceCategoryDto = new DeviceCategoryDto();
        deviceCategoryDto.setId(deviceCategoryIconSetVo.getId());
        deviceCategoryDto.setIconJson(deviceCategoryIconSetVo.getIconJson());
        deviceCategoryService.updateById(deviceCategoryDto);

        return Response.ok();
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @RequirePermissions(values = "deviceCategory:edit")
    @RequireTenants(values = SysConstants.SYS_TOP_TENANT_CODE)
    @Operation(summary = "修改单条设备种类记录")
    @Log(express = "修改设备种类")
    public Response<Object> update(@RequestBody @Validated DeviceCategoryDto deviceCategoryDto) {

        //填充基础数据
        fillEditCommonData(deviceCategoryDto);

        deviceCategoryService.updateById(deviceCategoryDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "deviceCategory:delete")
    @RequireTenants(values = SysConstants.SYS_TOP_TENANT_CODE)
    @Operation(summary = "删除存在的设备种类记录")
    @Log(express = "删除设备种类")
    public Response<Object> delete(@RequestBody Long[] ids) {

        deviceCategoryService.deleteBatch(ids);

        return Response.ok();
    }
}
